/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package semmatch;

import java.util.Iterator;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;


/**
 *
 * @author Aliz
 */
public class Main {

    /**
     * @param args the command line arguments
     */

    private static final String ONTOLOGY_DIR =
            "E:/Faculta/an4sem1/research/Apache2/htdocs/ontology/";
    private static final String SERVICE_DIR =
            "E:/Faculta/an4sem1/research/Apache2/htdocs/services/";

   public static void main(String[] args ) {
        // read the argument file, or the default
       Concept thing=new Concept("Thing",0);
       String dirStr = ONTOLOGY_DIR;
       File dir=new File(dirStr);
       String[] fisiere=dir.list();
       ClassHierarchy dc = new ClassHierarchy();
       for (int k=0;k<fisiere.length;k++){
        String source = "file:"+dirStr+fisiere[k];
        //System.out.println(source);
        OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM, null );

        // we have a local copy of the wine ontology
      //  addFoodWineAltPaths( m.getDocumentManager() );

        // read the source document
        m.read( source );
        
        try{
            for (Iterator<OntClass> i = m.listHierarchyRootClasses();  i.hasNext(); ) {
                // now list the classes
                
                OntClass cls=i.next();
                String myid;
                if (!cls.isAnon())
                    myid=cls.getLocalName();
                else
                    myid=cls.getId().toString();
                //System.out.println(cls.getLocalName());
                Concept c;
                if (!dc.containsKey(myid)){
                    //System.out.println(myid);
                    c=new Concept(myid,1);
                    c.addParent(0, thing);
                }
                else
                    c=dc.find(myid);
                dc.put(myid, c);
                dc.describeClass(cls);
                }
            for (Iterator<ObjectProperty> i = m.listObjectProperties();  i.hasNext(); ) {
                ObjectProperty p=i.next();
                dc.addRelation(p);
                 }
            }
        catch (Exception e)
                {
                //e.printStackTrace();
        }

   }
//           dc.listConcepts();
        String s1="Author";
        String s2="LandArea";
        Concept c1=dc.find(s1);
        Concept c2=dc.find(s2);
        System.out.println("Matching: "+dc.cm(c1,c2,1));

        ////////////////////////////////////////////
        double[][] test = {
            {1, 2, 3, 1},
            {3, 3, 3, 1},
            {3, 2, 3, 1},
            {4, 0.1, 6, 2}
        };
        //int[][] result = new HungarianAlgorithm().computeAssignments(test);
        int[][] result = HungarianAlgorithm.hgAlgorithm(test, "");
        for(int i=0; i<result.length; ++i){
            System.out.print(i + " : ");
            for(int j=0; j<result[i].length; ++j){
                System.out.print(result[i][j] + " ");
            }
            System.out.println();
        }

         //////////////////////////////////////
   //Citim un web service

        //WebService ws1 = new WebService(SERVICE_DIR + "academic-degreegovernmentorganization_funding_service.wsdl", dc);
        //System.out.println(ws1);
        
        List<WebService> services = new ArrayList<WebService>();
        String[] service_files = (new File(SERVICE_DIR)).list();
        for(String sf : service_files){
            if(!(new File(SERVICE_DIR + sf)).isDirectory()){
                WebService ws = new WebService(SERVICE_DIR + sf, dc);
                services.add(ws);
                System.out.println(ws);
            }
        }
        System.out.println("Read " + services.size() + " services");
        File f = new File("results.txt");
        try {
            FileOutputStream fop = new FileOutputStream(f);

            for(int i=0; i<services.size()-1; ++i){
                WebService ws1 = services.get(i);
                String str = ws1 + "\n";
                fop.write(str.getBytes());
                for(int j=i+1; j<services.size(); ++j){
                    WebService ws2 = services.get(j);
                    double m = ws1.match(ws2, dc);
                    str = "\t" + ws2 + " => " + m + "\n";
                    fop.write(str.getBytes());
                    //System.out.println(ws1 + " | " + ws2 + " => " + m);
                }
            }
            fop.close();
        } catch (Exception ex) {}
    }
        
        

  

   ////////////////////////////////////////
    // Internal implementation methods
    //////////////////////////////////

    /**
     * Add alternate paths to save downloading the default ontologies from the Web
     */
}
